clear
set more off

/*
Import dataset listing IP address of each respondent matched with likely state of resident (hand-collected)
*/

import excel "Survey2020.xlsx", sheet("IP") firstrow clear
duplicates drop IP, force
save IPaddresses.dta, replace

/*
Import survey dataset exported from Qualtrics
*/

import excel "Survey2020.xlsx", sheet("Data") firstrow clear
drop if _n==1|_n==2

/*
Clean main dataset and define variables
*/

*Drop observations from trial runs of the survey
drop if Status=="Survey Preview"
drop if _n<13 & (Q13=="Stay the Same" |Q173=="Stay the Same")

*Demographics
gen female=Q2=="Female"
drop Q2
rename Q3 age
destring age, force replace
gen ageSq=age^2
label var age "age"
rename Q4 ed
gen collegedegree=0 if ed=="Associate/Junior College degree (including academic, vocational, or occupational programs)"|ed=="High school diploma (or equivalent)"|ed=="Some college but no degree (including academic, vocational or occupational programs)"|ed=="Less than high school"
replace collegedegree=1 if collegedegree==. & ed!=""
rename Q5 inc
rename Q6 news
gen news_socialmedia=strpos(news, "Social")>0
gen news_print=strpos(news, "Print")>0
gen news_online=strpos(news, "Online")>0
gen news_radio=strpos(news, "Radio")>0
gen news_tv=strpos(news, "Television")>0
foreach stub in socialmedia print online radio tv {
	label var news_`stub' "`stub'"
}
gen news_onlinesocial=(news_online==1|news_socialmedia==1)
gen inccat=10 if strpos(inc, "$19,999")>0
replace inccat=20 if strpos(inc, "$29,999")>0
replace inccat=30 if strpos(inc, "$39,999")>0
replace inccat=40 if strpos(inc, "$49,999")>0
replace inccat=50 if strpos(inc, "$59,999")>0
replace inccat=60 if strpos(inc, "$74,999")>0
replace inccat=75 if strpos(inc, "$99,999")>0
replace inccat=100 if strpos(inc, "$100,000")>0
replace inccat=150 if strpos(inc, "$150,000")>0
replace inccat=200 if strpos(inc, "$200,000")>0
replace inccat=0 if strpos(inc, "Less than")>0
gen highincome=inccat>=75 if inccat!=.
label var highincome "highincome"
gen lowincome=inccat<=20
label var lowincome "lowincome"
gen stockowner=Q88=="Yes."
drop Q88
gen understandprobability=Q51=="100"
gen understandinterestrates=BP=="Less than today"
gen numerate=understandprobability==1&understandinterestrates==1 if understandprobabil!=. & understandint!=.

*Knowledge of inflation and inflation target
rename Q7 inflationknow
split inflationknow, p("(")
destring inflationknow1, force replace
drop inflationknow2 inflationknow
rename inflationknow1 inflationknow
rename Q9 targetresponse
gen target_notsure=(targetresponse=="Not sure") if targetresponse!=""
gen targetclaimtoknow=(target_notsure!=1) if targetresponse!=""
rename Q9_1_TEXT targetguess
gen targetknow=targetguess=="2"
destring Durationin targetguess, force replace

*Treatment group got info about coronavirus, control group did not
gen Treated=0 if Q40!=""
replace Treated=1 if Q40==""

*News and concern about coronavirus
gen CorNews=Q40
replace CorNews=Q84 if CorNews==""
drop Q40 Q84
gen CorNewsVery=CorNews=="Very closely."
gen CorNewsNo=CorNews=="Not closely at all."
rename Q85 CorConcernEconomy
rename Q86 CorConcernHealth
rename Q87 CorConcernFinance
gen CancelledTravel=1 if Q91=="Yes."
replace CancelledTravel=0 if Q91=="No."
gen MadePurchases=1 if Q92=="Yes."
replace MadePurchases=0 if Q92=="No."
drop Q91 Q92
egen CorNewsNumber=group(CorNews)

foreach stub in Economy Health Finance {
	egen Concern`stub'=group(CorConcern`stub')
	gen Concerned`stub'=Concern`stub'==2|Concern`stub'==3
	gen Unconcerned`stub'=Concern`stub'==1
	gen Highconcerned`stub'=Concern`stub'==3
}
gen CorConcernTotal=ConcernEc+ConcernHealth+ConcernFinance

*Follows stock and Fed news
rename Q52 stocknews
gen stocknewsveryclosely=stocknews=="Very closely."
gen stocknewsfollows=stocknews=="Very closely."|stocknews=="Somewhat closely"
label var stocknewsfollows "stocknews"
gen HeardFedNews=Q89=="Yes." if Q89!=""
gen KnowFedCut=Q90=="The Fed cut interest rates."
rename Q90_6_TEXT OtherFedNews

*Opinion of govt economic policy
rename Q41 governmentopinion
gen govgoodjob=governmentopinion=="Good Job" if governmentopinion!=""
gen govfairjob=governmentopinion=="Only Fair" if governmentopinion!=""
gen govpoorjob=governmentopinion=="Poor Job" if governmentopinion!=""
gen govgoodorfair=(govgoodjob==1|govfairjob==1)
gen govfairorpoor=(govfairjob==1|govpoorjob==1)

*Unemployment and inflation expectations

gen UnempUp1=(Q42=="More Unemployment") if Q42!=""
gen UnempDown1=(Q42=="Less") if Q42!=""
rename Q42 UnempE1
gen UnempE1Num=1 if UnempDown1==1
replace UnempE1Num=2 if UnempDown1==0 & UnempUp1==0
replace UnempE1Num=3 if UnempUp1==1
gen UnempUp2=(Q172=="More Unemployment") if Q172!=""
gen UnempDown2=(Q172=="Less") if Q172!=""
rename Q172 UnempE2
gen UnempE2Num=1 if UnempDown2==1
replace UnempE2Num=2 if UnempDown2==0 & UnempUp2==0
replace UnempE2Num=3 if UnempUp2==1
gen UnempMorePessimistic=(UnempUp2==1 & UnempUp1==0)|(UnempDown1==1& UnempDown2==0) if UnempUp1!=. & UnempUp2!=.
gen UnempMoreOptimistic=(UnempUp1==1 & UnempUp2==0)|(UnempDown2==1& UnempDown1==0) if UnempUp1!=. & UnempUp2!=.

gen RevUNum=1 if UnempMorePes==1
replace RevUNum=2 if UnempMorePes==0 & UnempMoreO==0 & UnempUp1!=. & UnempUp2!=.
replace RevUNum=3 if UnempMoreO==1
label define revu 1 "More pessimistic" 2 "No change" 3 "More optimistic"
label values RevUNum revu
destring Q15_2_TEXT Q16_1_TEXT Q17_1_TEXT Q18_1_TEXT Q175_2_TEXT Q176_1_TEXT Q177_1_TEXT Q178_1_TEXT, force replace
foreach stub in Q16_1_TEXT Q18_1_TEXT Q176_1_TEXT Q178_1_TEXT {
	replace `stub'=`stub'*-1
}


egen PiE1=rowmin(Q15_2_TEXT Q16_1_TEXT Q17_1_TEXT Q18_1_TEXT)
replace PiE1=0 if Q13=="Stay the Same" & Q14=="Will Not Go Up"
egen PiE2=rowmin(Q175_2_TEXT Q176_1_TEXT Q177_1_TEXT Q178_1_TEXT)
replace PiE2=0 if Q173=="Stay the Same" & Q174=="Will Not Go Up"
gen uncensoredPiE1=PiE1
gen uncensoredPiE2=PiE2
replace PiE1=. if PiE1>=50|PiE1<=-50
replace PiE2=. if PiE2>=50|PiE2<=-50
gen RevPi=PiE2-PiE1
gen Rev0=RevPi==0 if RevPi!=.
gen RevPiUp=RevPi>0 if RevPi!=.
gen RevPiDown=RevPi<0 if RevPi!=.
gen PiE1DK=PiE1==.
gen PiE2DK=PiE2==.
gen RevPiNum=1 if RevPiDown==1
replace RevPiNum=2 if Rev0==1
replace RevPiNum=3 if RevPiUp==1
forvalues j=1/2 {
gen M5`j'=(PiE`j'/5==floor(PiE`j'/5)) if PiE`j'!=.
gen M5orDK`j'=M5`j'==1|PiE`j'DK==1
}
label define revpi 1 "Down" 2 "No change" 3 "Up"
label values RevPiNum revpi
*Confidence in Fed and Trump
rename Q78_1 confidencetrump
rename Q78_2 confidencefed
foreach stub in trump fed {
	gen highconfidence_`stub'=(confidence`stub'=="A great deal of confidence") if confidence`stub'!=""
	gen almostnoconfidence_`stub'=(confidence`stub'=="Almost no confidence") if confidence`stub'!=""
	gen confidence_`stub'=(confidence`stub'=="A great deal of confidence"|confidence`stub'=="A fair amount of confidence") if confidence`stub'!=""
	
	gen conf`stub'=1 if confidence`stub'=="Almost no confidence"
	replace conf`stub'=2 if confidence`stub'=="Only a little confidence"
	replace conf`stub'=3 if confidence`stub'=="A fair amount of confidence"
	replace conf`stub'=4 if confidence`stub'=="A great deal of confidence"
}

gen chairknow=Q50=="Jerome Powell"

*Drop respondents that took less than 2 minutes
drop if Dur<120


*Weights
gen ID = _n+10000

quietly sum female
gen wtfemale=0.508/r(mean)
gen wtmale=(1-0.508)/(1-r(mean))
forvalues j=0(10)60 {
	gen inc`j'=inccat==`j'
	quietly sum inc`j'
	gen meaninc`j'=r(mean)*100
}
gen inc75=(inccat==75)
quietly sum inc75
gen meaninc75=r(mean)*100

gen inc100=(inccat==100)
quietly sum inc100
gen meaninc100=r(mean)*100

gen inc150=(inccat==150)
quietly sum inc150
gen meaninc150=r(mean)*100

gen inc200=(inccat==200)
quietly sum inc200
gen meaninc200=r(mean)*100

gen wt1=wtfemale if female==1
replace wt1=wtmale if female==0
gen wt2=5/meaninc0 if inccat==0
replace wt2=9/meaninc10 if inccat==10
replace wt2=9/meaninc20 if inccat==20
replace wt2=9/meaninc30 if inccat==30
replace wt2=7/meaninc40 if inccat==40
replace wt2=8/meaninc50 if inccat==50
replace wt2=10/meaninc60 if inccat==60
replace wt2=12/meaninc75 if inccat==75
replace wt2=15/meaninc100 if inccat==100
replace wt2=7/meaninc150 if inccat==150
replace wt2=9/meaninc200 if inccat==200
gen weight=wt1*wt2

merge m:1 IP using IPaddresses.dta
drop _merge
gen WHOstate=State=="Arizona"|State=="California"|State=="Florida"|State=="Georgia"|State=="Illinois"|State=="Massachusetts" ///
|State=="New Hampshire"|State=="New York"|State=="New Jersey"|State=="North Carolina"|State=="Oregon"|State=="Rhode Island"|State=="Texas"|State=="Washington"|State=="Wisconsin"

gen S=WHOs
gen S_Treated=S*Tr
gen govopinion=1 if govpoorjob==1
replace govopinion=2 if govfairjob==1
replace govopinion=3 if govgoodjob==1

*Keep and label the variables that will be used in analysis
keep age ageSq female numerate stockowner stocknewsfollows collegedegree highincome lowincome news_socialmedia news_print news_online news_radio news_tv weight govopinion govgoodjob govpoorjob ///
CorNewsNum ConcernH ConcernE ConcernF CancelledT MadeP Treated CorConcernEconomy UnempDown1 UnempUp1 PiE1 M51 KnowFedCut UnempE1Num RevPiNum RevUNum S S_Treated conftrump conffed targetknow chairknow ///
RevPiDown Rev0 RevPiUp Duration RevPi
foreach stub in ageSq weight female collegedegree stockowner numerate Treated CancelledTravel MadePurchases govgoodjob govpoorjob KnowFedCut ConcernEconomy ConcernHealth ConcernFinance {
	label var `stub' "`stub'"
}
label var CorConcernEconomy "Coronavirus-related concern about economy"
label var CorNewsNum "How closely following coronavirus-related news"
label var UnempUp1 "Initially expects unemployment to rise"
label var UnempDown1 "Initially expects unemployment to fall"
label var UnempE1Num "Initial unemployment expectation (categorical)"
label var RevUNum "Revision of unemployment expectation (categorical)"
label var PiE1 "Initial inflation expectation"
label var RevPiNum "Revision of inflation expectation (categorical)"
label var M51 "Initial inflation expectation is multiple of 5%"
label var S "In a state mentioned in info treatment"
label var S_Treated "Interaction of S and Treated"
label var govopinion "Opinion of government economic policy"
label var targetknow "Knows inflation target"
label var chairknow "Knows Fed Chair"
label var conftrump "Confidence in Trump"
label var conffed "Confidence in Fed"
label var Duration "Survey duration (seconds)"
label var RevPiUp "Revises expected inflation up"
label var RevPiDown "Revises expected inflation down"
label var Rev0 "Makes no revision to expected inflation"
label var RevPi "Revision to expected inflation"
save AnalysisData.dta, replace
